<template>
  <div class="details">
    <div class="details-content">
      <div class="details-content-left">
        <el-select
          class="projectValue"
          v-model="headerForm.projectId"
          placeholder="请选择"
          @change="headerSearch"
        >
          <el-option
            v-for="item in projectOption"
            :key="item.id"
            :label="item.projectName"
            :value="item.id"
          />
        </el-select>
        <div class="userData">
          <div
            v-for="(item, index) in userList.list"
            :key="index"
            :class="`userItem ${userActive === index ? 'userActive' : ''}`"
            @click="userListClick(index, item)"
          >
            <div>{{ item.userName }}</div>
          </div>
        </div>
      </div>
      <div class="details-content-right">
        <el-form
          :inline="true"
          :model="headerForm"
        >
          <el-form-item label="起止时间">
            <el-date-picker
              v-model="headerForm.date"
              type="daterange"
              clearable
              format="YYYY/MM/DD"
              value-format="YYYY-MM-DD 00:00:00"
              style="width: 239px"
            />
          </el-form-item>
          <el-form-item label="用户名">
            <el-input
              v-model="headerForm.userName"
              placeholder="请输入"
              clearable
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="房间号">
            <el-input
              v-model="headerForm.houseNum"
              placeholder="请输入"
              clearable
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="reGetList"
            >
              查询
            </el-button>
            <!-- <el-button @click="onExport"> 导出 </el-button>
            <el-button @click="onPrint"> 打印 </el-button> -->
          </el-form-item>
        </el-form>
        <el-table
          :data="tableData.list"
          stripe
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="编号"
            width="80"
          />
          <el-table-column
            prop="userName"
            label="用户名"
          />
          <el-table-column
            prop="houseNum"
            label="房间号"
          />
          <el-table-column
            prop="billFee"
            label="账单费用"
          />
          <el-table-column
            prop="billDate"
            label="账单时间"
          />
          <el-table-column
            prop="elecFee"
            label="电费"
          />
          <el-table-column
            prop="waterFee"
            label="水费"
          />
          <el-table-column
            prop="gasFee"
            label="气费"
          />
          <el-table-column
            fixed="right"
            label="操作"
          >
            <template #default="{ row }">
              <el-button
                link
                type="primary"
                size="small"
                @click="onDetail(row.id)"
              >
                <svg-icon
                  name="xqz"
                  color="#00AA86"
                  style="font-size: 12px; margin-right: 2px"
                />
                详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :pageProps="pageProps"
          layout="prev, pager, next"
          @current-change="getList"
        />
      </div>
    </div>
  </div>
  <DetailedDialog ref="refDetailedDialog" />
</template>

<script setup lang="ts">
/**
 * 账单明细
 */
import { reactive, ref, nextTick } from "vue";
import { ElMessage, ElLoading } from "element-plus";
import { useRoute } from "vue-router";
import {
  getProjectList,
  getUserList,
  getBillDetailList,
  exportData,
} from "@/api/settle";
import Pagination from "@/components/Pagination/Index.vue";
import DetailedDialog from "./components/DetailedDialog.vue";

const route = useRoute();
const projectOption = ref([]);
const headerForm = reactive({
  projectId: null,
  userId: null,
  houseNum: "",
  userName: "",
  date: [],
  startDate: "",
  endDate: "",
});
const refDetailedDialog = ref(null);

getProjectList()
  .then((res: any) => {
    projectOption.value = res.data;
  })
  .catch((err: any) => {
    console.error("err", err.msg);
  });

const userList = reactive({
  list: [] as any,
});
const userActive = ref(null);

const headerSearch = () => {
  headerForm.userId = "";
  getUsers();
  reGetList();
};

const getUsers = () => {
  const params = {
    pageNum: 1,
    pageSize: 100,
    projectId: headerForm.projectId,
  };
  getUserList(params)
    .then((res) => {
      userList.list = res.data.records;
    })
    .catch((err) => {
      console.error("err", err);
    });
};

const userListClick = (index: any, item: any) => {
  // 房间列表点击事件
  userActive.value = index;
  headerForm.userId = item.id;
  reGetList();
};

const pageProps = ref({
  pageNum: 1,
  pageSize: 20,
  total: 0,
});

const tableData = reactive({
  list: [] as any,
});

const getList = () => {
  const params = {
    pageNum: pageProps.value.pageNum,
    pageSize: pageProps.value.pageSize,
    ...headerForm,
  };
  if (headerForm.date[0] && headerForm.date[1]) {
    params.startDate = headerForm.date[0];
    params.endDate = headerForm.date[1];
  }
  delete params.date;
  getBillDetailList(params)
    .then((res) => {
      tableData.list = res.data.records;
      pageProps.value.total = res.data.total;
    })
    .catch((err: any) => {
      console.error("err", err.msg);
    });
};
const reGetList = () => {
  pageProps.value.pageNum = 1;
  getList();
};

// 导出
const onExport = () => {
  const loading2 = ElLoading.service({
    lock: true,
    text: "导出中，请稍等...",
    background: "rgba(0, 0, 0, 0.7)",
  });
  exportData({
    pageNum: pageProps.value.pageNum,
    pageSize: pageProps.value.pageSize,
    ...headerForm,
  })
    .then((res: any) => {
      const link = document.createElement("a");
      let blob = new Blob([res.data], {
        type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      });
      link.style.display = "none";
      link.href = URL.createObjectURL(blob);
      let fileName = res.headers["content-disposition"]
        .split(";")[1]
        .split("filename=")[1];
      link.download = decodeURIComponent(fileName);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      loading2.close();
    })
    .catch(() => {
      loading2.close();
      ElMessage({
        message: "导出失败",
        type: "error",
      });
    });
};

const onPrint = () => {
  console.log("headerForm", headerForm);
};

const onDetail = (id: string) => {
  refDetailedDialog.value.openHandle(id);
};

const initPage = () => {
  const name = route.query?.name;
  if (name) headerForm.userName = name;
  nextTick(() => {
    getList();
  });
};

initPage();
</script>

<style lang="scss" scoped>
.details {
  padding: 20px;
  background: #f2fbf9;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.details-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  height: 40px;

  .details-header-title {
    font-weight: bold;
    color: #333333;
    font-size: 16px;
    display: flex;
    align-items: center;

    .el-icon {
      margin-right: 4px;
    }
  }
}

.details-content {
  position: relative;
  min-height: calc(100vh - 130px);
  padding: 24px 16px 24px 0px;
  background: #ffffff;
  box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.06);
  border-radius: 8px;
  .details-content-left {
    position: absolute;
    width: 216px;
    height: 100%;
    top: 0;
    bottom: 0;
    left: 0;
    border-right: 1px solid #eff5f4;
    .roomData {
      overflow: auto;
    }
    .projectValue {
      width: 184px;
      margin: 16px 16px 11px;
    }

    .userItem {
      width: 100%;
      height: 32px;
      line-height: 32px;
      cursor: pointer;
      div {
        position: relative;
        margin-left: 40px;
        &::after {
          position: absolute;
          top: 12px;
          left: -16px;
          width: 8px;
          height: 8px;
          border-radius: 4px;
          border: 1px solid #c5c8c7;
          content: "";
        }
      }
    }

    .userActive {
      background: rgba(204, 245, 236, 0.4);
      border-right: 1px solid #03cca1;
      div {
        color: #00aa86;

        &::after {
          border-color: #03cca1;
          background: #03cca1;
        }
      }
    }
  }

  .details-content-right {
    margin-left: 216px;
    padding-left: 15px;
    .el-form-item {
      margin-right: 20px;
    }
  }
}
</style>
